#include "PlatformProperty.h"

MAPPING_VALUE(DoorControlType, sdb::DoorOpenerReq1, sdb::DoorOpenerReq1::DoorOpenerCls)
    MAPPING_ITEM(DoorControlType::DOOR_CLOSE, sdb::DoorOpenerReq1::DoorOpenerCls)
    MAPPING_ITEM(DoorControlType::DOOR_OPEN, sdb::DoorOpenerReq1::DoorOpenerOpen)
    MAPPING_ITEM(DoorControlType::DOOR_PAUSE, sdb::DoorOpenerReq1::DoorOpenerStop)
MAPPING_END

SYSTEM_READ_WRITE_PROPERTY(DOOR_MOVE, int32_t) {
    // [SW_FC_009_012] Vehicle 车门-电动尾门
    builder.area(VehicleAreaDoor::REAR)
            .initValue([]() { return  toInt(sdb::TrOpenerSts1::FullClsd); })
            .withStatusBySignals(
                    []([[maybe_unused]] const PropertyStatus &last) {
                        auto carmod = SIGNAL.getVehModMngtGlbSafe1CarModSts1();
                        auto usgmod = SIGNAL.getVehModMngtGlbSafe1UsgModSts();
                        auto cc98 = SIGNAL.getcarconfig98();
                        sdb::TrOpenerSts1 sTrOpenerSts1 = SIGNAL.getTrOpenerSts();
                        sdb::Timeout sTrOpenerStsTimeout = SIGNAL.getTrOpenerStsTimeout();
                        VHALOG.d("DOOR_MOVE status Change carmod[%d], usgmod[%d], cc98[%d], sTrOpenerSts1[%d], sTrOpenerStsTimeout[%d]", carmod,
                              usgmod, cc98, toInt(sTrOpenerSts1), toInt(sTrOpenerStsTimeout));
                        if (!SIGNAL.carConfigIs(SignalRX::carconfig98, sdb::CarConfig98::_01)) {
                            if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd) &&
                                SIGNAL.carModeIs(sdb::CarModSts1::CarModNorm, sdb::CarModSts1::CarModFcy,
                                                 sdb::CarModSts1::CarModCrash,
                                                 sdb::CarModSts1::CarModDyno) &&
                                sTrOpenerSts1 != sdb::TrOpenerSts1::Ukwn &&
                                sTrOpenerStsTimeout != sdb::Timeout::Timeout) {
                                VHALOG.d("DOOR_MOVE PropertyStatus Active");
                                return PropertyStatus::Active;
                            }
                            VHALOG.d("DOOR_MOVE PropertyStatus NotActive");
                            return PropertyStatus::NotActive;
                        }
                        VHALOG.d("DOOR_MOVE PropertyStatus NotAvailable");
                        return PropertyStatus::NotAvailable;
                    },
                    SignalRX::VehModMngtGlbSafe1UsgModSts,
                    SignalRX::VehModMngtGlbSafe1CarModSts1,
                    SignalRX::carconfig98,
                    SignalRX::TrOpenerSts,
                    SignalRX::TrOpenerStsTimeout)
            .withValueBySignals(
                    []([[maybe_unused]] const int32_t &last) {
                        VHALOG.d("DOOR_MOVE Valve Change last:[%d]", last);
                        int32_t sTrOpenerSts1 = toInt(SIGNAL.getTrOpenerSts());
                        VHALOG.d("DOOR_MOVE Valve sTrOpenerSts1:[%d]", sTrOpenerSts1);
                        return sTrOpenerSts1;
                    },
                    SignalRX::TrOpenerSts)
            .onSetProperty(
                    [](const int32_t &value) {
                        if (value !=  toInt(DoorControlType::DOOR_CLOSE)
                                && value !=  toInt(DoorControlType::DOOR_OPEN)
                                && value !=  toInt(DoorControlType::DOOR_PAUSE)) {
                            VHALOG.d("DOOR_MOVE : param invaild");
                            return;
                        }
                        VHALOG.d("DOOR_MOVE : setValue[%d]", value);
                        if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd)) {
                            VHALOG.d("DOOR_MOVE : setCB_VFC_SetVehMisc");
                            SIGNAL.setCB_VFC_SetVehMisc(sdb::OnOff1::On);
                        }
                        SIGNAL.setTrunkOpenHmiReq(mappingValue(DoorControlType(value), sdb::DoorOpenerReq1::DoorOpenerCls));
                    });
}